package com.slt.android.customview.dragview;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Scroller;

/**
 * Created by dp on 2017/12/21.
 */

public class DragView extends View {

    Scroller mScroller;
    public DragView(Context context) {
        super(context);
        init(context);
    }

    public DragView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public DragView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context){
        mScroller = new Scroller(context);
    }


    int lastX,lastY;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                lastX = x;
                lastY = y;
                break;
            case MotionEvent.ACTION_MOVE:
                int offsetX = x - lastX;
                int offsetY = y - lastY;

//                /* 方式1*/layout(getLeft() + offsetX , getTop()+offsetY , getRight() + offsetX , getBottom() + offsetY);
//                 /* 方式2*/
//                          offsetLeftAndRight(offsetX);
//                          offsetTopAndBottom(offsetY);
                    /* 方式3*/
//                RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) getLayoutParams();
//                lp.leftMargin = getLeft()+offsetX;
//                lp.topMargin = getTop()+offsetY;
//                setLayoutParams(lp);
                /* 方式4*/
//                ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) getLayoutParams();
//                mlp.topMargin = getTop()+offsetY;
//                mlp.leftMargin = getLeft()+offsetX;
//                setLayoutParams(mlp);
//                 /* 方式5(对view调用scrollby 参数为正数，则在Android坐标系中往负方向移动)*/
//              ((View)getParent()).scrollBy(-offsetX , -offsetY);
                    /* 方式6 scrollTo*/
//                ((View)getParent()).scrollTo( - offsetX -getLeft(), - offsetY - getTop());
                /* 方式7 mScroller*/
                 /* 方式8 viewDragHelper*/
                /* 方式9 属性动画*/

                break;
            case MotionEvent.ACTION_UP:
                //手指离开时执行滑动过程
                View viewGroup = (View) getParent();
                int sx = viewGroup.getScrollX();
                int sy = viewGroup.getScrollY();
                mScroller.startScroll(sx , sy , -sx , -sy);
                invalidate();
                break;
        }

        return true;
    }

    /** 在测试scroller的时候用到*/
    @Override
    public void computeScroll() {
        super.computeScroll();
        //判断Scroller是否执行完毕--是否完成了滑动
        if(mScroller.computeScrollOffset()){
            ((View)getParent()).scrollTo(mScroller.getCurrX() , mScroller.getCurrY());
            //通过不断的重绘来不断调用computeScroll
            invalidate();
        }
    }
}
